<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
            >新增角色</el-button>
            <!-- 表格 -->
            <el-table v-loading="loading" :data="list">
              <el-table-column type="index" :index="indexMethod" label="序号" width="100" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="scoped">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(scoped.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <!-- 公司信息 -->
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <el-pagination
      layout="total,sizes,prev, pager, next"
      :total="total"
      :page-size="FormData.pagesize"
      :page-sizes="[5, 8, 9, 10]"
      @current-change="currentChange"
      @size-change="sizeChange"
    />
  </div>
</template>

<script lang="ts">
import { getRoleListApi, deleteRoleApi } from '@/api/setting'

export default ({
  name: 'Setting',
  data() {
    return {
      list: [],
      FormData: {
        page: 1, // 当前页码数
        pagesize: 5 // 当前页面需要的数据条数
      },
      total: 0,
      loading: false
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 删除
    delRole(id) {
      this.$confirm('此操作将永久删除该文件', '温馨提示').then(async() => {
        await deleteRoleApi(id)
        if (this.list.length === 1 && this.page > 1) {
          this.page--
        }
        this.getRoleList()
        this.$message.success('删除成功')
      }).catch((error) => {
        console.log(error)
      })
    },
    // 自定义索引
    indexMethod(index) {
      return (this.FormData.page - 1) * this.FormData.pagesize + index + 1
    },
    // 	每页条数
    sizeChange(val) {
      this.FormData.pagesize = val
      this.getRoleList()
    },
    // 当前页
    currentChange(val) {
      this.FormData.page = val
      this.getRoleList()
    },
    async getRoleList() {
      this.loading = true
      const res = await getRoleListApi(this.FormData)
      this.list = res.data.rows
      this.total = res.data.total
      this.loading = false
    }
  }
})
</script>

<style scoped lang="scss">
.el-pagination{
  display: flex;
  justify-content: right;
  // margin-right: 30px;
}
</style>
